<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/5/9
 * Time: 17:02
 */

namespace Biz\module\Classes\mgt;

use Biz\model\trade\OrderRefundQueue as mOrderRefundQueue;
use Biz\pockets\excel\DownExcel;

class OrderRefundQueueExcel
{
    private static $fileName = '退款订单列表'; //Excel文件名
    private static $sheetTitle = '好好住订单'; //Excel的sheet标题

    /**
     * 订单下载
     * @throws \Exception
     */
    public static function downList($field, $orderPayWhere)
    {

        $orderRefundQueue = mOrderRefundQueue::getOrderRefundQueue($field, $orderPayWhere);

        if (!$orderRefundQueue) {
            throw new \Exception('暂无可导出数据', 0);
        }

        $orderStatus      = mOrderRefundQueue::$orderReturnStatus;
        $orderRefundWay   = mOrderRefundQueue::$payWayDesc;
        $orderRefundQueue = self::formatOrderPay($orderRefundQueue, $orderStatus, $orderRefundWay);
        $header = ['支付渠道', '退款时间', '退款单号', '退款金额', '状态', '好好住订单号'];
        $letter = range('A', 'F');
        $excelObj = new DownExcel();
        $excelObj->setFileName(self::$fileName);
        $excelObj->setSheetTitle(self::$sheetTitle);
        //设置列宽
        $widths = ['A' => 20, 'B' => 30, 'C' => 50, 'D' => 10, 'E' => 20, 'F' => 50];
        $excelObj->setWidth($widths);
        //默认单元格居中
        $excelObj->setDefaultStyleAlignment();
        $excelObj->setTextFormat('C'); //支付平台交易号防止为科学计数法
        $excelObj->setTextFormat('F'); //好好住订单号防止为科学计数法
        foreach ($header as $key => $value) {
            //设置字体
            $excelObj->setFrontName($letter[$key]);
            //设置加粗
            $excelObj->setFrontBold($letter[$key]);
            //设置大小
            $excelObj->setFrontSize($letter[$key]);
        }

        $excelObj->create($orderRefundQueue, $header);
    }

    protected static function formatOrderPay($orderPay, $orderStatus, $orderRefundWay)
    {
        $order = [];
        foreach ($orderPay as $orderPay_k => $orderPay_v) {
            foreach ($orderStatus as $orderStatus_k => $orderStatus_v) {

                if ($orderPay_v['refund_rmb_status'] == $orderStatus_k) {
                    $orderPay[$orderPay_k]['refund_rmb_status'] = $orderStatus_v;
                }
            }

            foreach ($orderRefundWay as $orderRefundWay_k => $orderRefundWay_v){
                if($orderPay_v['refund_rmb_way'] == mOrderRefundQueue::REFUND_WAY_WXPAY_APP || $orderPay_v['refund_rmb_way'] == mOrderRefundQueue::REFUND_WAY_MINIPROGRAM || $orderPay_v['refund_rmb_way'] == mOrderRefundQueue::REFUND_WAY_TUAN_MINIPROGRAM) {
                    $orderPay[$orderPay_k]['refund_rmb_way'] = mOrderRefundQueue::$payWayDesc[mOrderRefundQueue::REFUND_WAY_WXPAY_APP];
                } else if ($orderPay_v['refund_rmb_way'] == $orderRefundWay_k) {
                    $orderPay[$orderPay_k]['refund_rmb_way'] = $orderRefundWay_v;
                }
            }
        }

        return $orderPay;
    }

}